<?php
/**
 * Semester ClassController
 * Semester Class Information Management
 *
 * semester entry
 *
 * @version $Id: ClassController.php 77 2010-04-25 07:28:48Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package semester
 * @subpackage Controller
 */
class semester_ClassController extends Zend_Controller_Action
{

    function indexAction(){
        $this->_redirect('semester/class/set');
    }



    /**
     * 設定班級數
     *
     */

    function setAction()
    {
        //學年學期
        if (!$this->_request->getParam('year_semester'))
        $year_semester = $this->view->Semester['year'].'-'.$this->view->Semester['semester'];
        else
        $year_semester = $this->_request->getParam('year_semester');

        $arr = explode("-",$year_semester);
        $year = (int) $arr[0];
        $semester = (int) $arr[1];
        $this->view->year_semester = $year_semester;
        $this->view->year = $year;
        $this->view->semester = $semester;

        $semesterClass = new Semester_Model_SemesterClass();

        if ($this->_request->isPost() and $this->_request->getParam('act')){
            $cNameKind = $this->_request->getParam('c_name_kind');
            $cNum = $this->_request->getParam('c_num');
            $carr = $semesterClass->getClassCName();
            foreach($cNum as $id=>$val) {
                $cNameArr = $carr[$cNameKind[$id]];
                // 班級數如超過，預設以 一、二、三..為班名
                if ($val>count($cNameArr))
                $cNameArr = $carr[1];

                //先將所有班級 enable 設為N
                //再依新設定的班級數目,逐筆查詢
                //如已存在則 enable 重設為'y'
                //如不存在則新增一個班級資料

                $q = Doctrine_Query::create()
                ->update('Semester_Model_SemesterClass')
                ->set('enable','?','n')
                ->where('year=? AND semester=? AND enable=? AND grade=?',
                array($year, $semester, 'Y', $id));
                $q->execute();

                for($i=0;$i<$val;$i++) {
                    $y=$i+1;
                    $q = Doctrine_Query::create()
                    ->from('Semester_Model_SemesterClass')
                    ->where('year=? AND semester=?  AND grade=? AND class_sn=?',
                    array($year, $semester,  $id, $y));
                    $semesterClass = new Semester_Model_SemesterClass();
                    if ($row = $q->fetchOne(null, Doctrine_Core::HYDRATE_ARRAY)) {
                        $semesterClass->assignIdentifier($row['sn']);
                        $semesterClass->enable = 'y';
                    }else {
                        $semesterClass->year= $year;
                        $semesterClass->semester = $semester;
                        $semesterClass->grade = $id;
                        $semesterClass->class_sn = $y;
                        $semesterClass->enable = 'y';
                    }
                    if ($cNameArr)
                    $semesterClass->chinese_name = $cNameArr[$y];
                    else
                    $semesterClass->chinese_name= '';


                    $semesterClass->save();
                }
            }
        }

        // 設定的班級數
        $this->view->num_data = $semesterClass->getClassSetNum($year,$semester);
        // 設定的班級名稱
        $this->view->name_arr = $semesterClass->getClassSetName($year,$semester);
        // 設定的班級名稱類型
        $this->view->classNameKind = $semesterClass->getClassNameKind();

    }

    function editAction()
    {
        $this->_helper->json('set ok');
    }

    /**
     * 取得年級班名資料
     * 以 Json 格式送出
     */

    function getyearAction()
    {

        $year= (int)$this->_request->getParam('year');
        $semester = (int)$this->_request->getParam('semester');
        $id = (int)$this->_request->getParam('id');

        $q = Doctrine_Query::create()
        ->from('Semester_Model_SemesterClass')
        ->where('year=?',$year)
        ->andWhere('semester=?', $semester)
        ->andWhere('grade=?', $id)
        ->andWhere('enable=?', 'y')
        ->orderby('class_sn');

        $row = $q->fetchArray();
        $this->_helper->Json($row);
    }

    /**
     * 設定年級名稱
     *
     */

    function setyearAction(){

        $sn = $this->_request->getParam('sn');

        foreach($sn as $id=>$val){
            $semesterClass = new Semester_Model_SemesterClass();
            $semesterClass->assignIdentifier($id);
            $semesterClass->chinese_name = $val;
            $semesterClass->save();
        }
        $this->_helper->Json('saved');
    }

}